package org.ohos.sample.cordova;

import ohos.agp.components.webengine.Navigator;
import ohos.agp.components.webengine.WebConfig;
import ohos.agp.components.webengine.WebView;
import ohos.app.Context;

public class SystemWebViewEngine extends WebViewEngine {

    private WebView mWebView;

    public SystemWebViewEngine(Context context, WebViewInterface webViewInterface) {
        super(context, webViewInterface);
    }

    public void init(WebView webView){
        mWebView = webView;
        settingsWebView();
    }
    private void settingsWebView(){
        WebConfig webConfig = mWebView.getWebConfig();
        webConfig.setJavaScriptPermit(true);
        webConfig.setDataAbilityPermit(true);
        webConfig.setWebStoragePermit(true);
        webConfig.setViewPortFitScreen(true);
//        webConfig.setLocationPermit(true);
        mWebView.setWebAgent(new SystemWebAgent(webViewInterface));
        mWebView.setBrowserAgent(new SystemBrowserAgent(getContext(),webViewInterface));
    }    @Override
    public void load(String url) {
        mWebView.load(url);
    }

    @Override
    public boolean canGoBack() {
        Navigator navigator = mWebView.getNavigator();
       return navigator.canGoBack();
    }

    @Override
    public void goBack() {
        Navigator navigator = mWebView.getNavigator();
        if (navigator.canGoBack()) {
            navigator.goBack();
        }
    }

    @Override
    public void goForward() {
        Navigator navigator = mWebView.getNavigator();
        if (navigator.canGoForward()) {
            navigator.goForward();
        }
    }

    @Override
    public boolean canGoForward() {
        Navigator navigator = mWebView.getNavigator();
        if (navigator.canGoForward()) {
            return true;
        }
        return  false;
    }

    @Override
    public void destroy() {
        if(mWebView!=null){
            mWebView.load("about:blank");
        }
    }
}
